package cn.lingyangwl.agile.system.manager;

import cn.lingyangwl.agile.common.datasource.TenantDeleteMQ;
import cn.lingyangwl.agile.common.datasource.UserDeleteMQ;
import cn.lingyangwl.agile.system.model.mq.ResourceSyncMQ;
import cn.lingyangwl.agile.system.model.entity.RamModule;
import cn.lingyangwl.agile.system.model.entity.SysTenant;
import cn.lingyangwl.agile.system.model.mq.TenantUpdateMQ;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shenguangyang
 */
@Component
public class SysMqManager {
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送资源模块刷新消息
     */
    public void sendResourceModuleRefresh(List<RamModule> list) {
        rabbitTemplate.convertAndSend(
                ResourceSyncMQ.EXCHANGE_REFRESH, ResourceSyncMQ.KEY_NOTICE_REFRESH, new ResourceSyncMQ(list).toMessage()
        );
    }

    /**
     * 发出删除用户消息
     */
    public void sendDeleteUserMsg(Long userId) {
        UserDeleteMQ mq = UserDeleteMQ.build(userId);
        rabbitTemplate.convertAndSend(UserDeleteMQ.EXCHANGE, UserDeleteMQ.KEY, mq.toMessage());
    }

    /**
     * 发出删除租户消息
     */
    public void sendDeleteTenantMsg(SysTenant tenant) {
        TenantDeleteMQ mq = TenantDeleteMQ.build(tenant.getIsMainTenant(), tenant.getId());
        rabbitTemplate.convertAndSend(TenantDeleteMQ.EXCHANGE, TenantDeleteMQ.KEY, mq.toMessage());
    }

    /**
     * 发出更新租户消息
     */
    public void sendUpdateTenantMsg(SysTenant tenant) {
        TenantUpdateMQ mq = TenantUpdateMQ.build(tenant.getId(), tenant.getPackageId(), tenant.getDomain());
        rabbitTemplate.convertAndSend(TenantUpdateMQ.EXCHANGE, TenantUpdateMQ.KEY, mq.toMessage());
    }
}
